package com.panfeng.xcloud.gateway.filters;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;

@Slf4j
public class TimerCounter {

    private static final ThreadLocal<Long> apiDuration = new ThreadLocal<>();

    public static void before(HttpServletRequest request){
        Long startTime = System.currentTimeMillis();
        apiDuration.set(startTime);
        String url = request.getRequestURI();
        log.info(">>> 请求计时器-开始计时-请求URL:{} =>开始时间：{} <<<" ,url ,startTime);
    }

    public static void after(HttpServletRequest request){
        Long startTime = apiDuration.get();
        startTime = null != startTime ? startTime : 0L;
        Long endTime =  System.currentTimeMillis();
        String url = request.getRequestURI();
        log.info(">>> 请求计时器-结束计时-请求URL:{} =>开始时间：{} =>结束时间：{} =>耗时：{}毫秒 <<<",url ,startTime ,endTime ,(endTime -  startTime));
    }
}
